// 项目初始化数据-项目运行只走一次
import { defineStore } from "pinia"
import { getToday } from '@/utils/index';

interface IToDay {
    date: string,
    week: string
}

interface IState {
    toDay: IToDay
}

export const useInitStore = defineStore({
    // 每个 store 的 id 必须唯一
    id: 'init',
    // state 表示数据源
    state: (): IState => ({
        toDay: {
            date: "",
            week: ""
        }
    }),
    // getters 类似于 computed，可对 state 的值进行二次计算
    getters: {

    },
    // actions 用来修改 state
    actions: {
        getToday() {
            // 间隔
            const interval = 1000 * 60 * 5

            // 获取当天数据
            const patch = ()=>{
                this.$patch((state) => {
                    state.toDay = getToday()
                })
            }

            // 定时调用
            patch()
            setInterval(() => {
                patch()
            }, interval)
        }
    },

})